package com.yqkj.zysoft.upms.service.impl;

import com.project.easy.common.framework.auth.core.IQueryAuth;
import  com.project.easy.common.util.collection.CollectionUtil;
import  com.project.easy.common.util.dto.AuthDto;
import  com.project.easy.common.util.dto.UserInfo;
import com.yqkj.zysoft.upms.entity.dto.FunctionAuthDto;
import com.yqkj.zysoft.upms.mapper.FrameworkUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author GreatorMan.cool
 * @copyright: Copyright (c) 2020
 * @company: 扬起科技有限公司
 * @date 2021/10/26 22:10
 * @description:用户权限查询
 */
@Service
public class UpmsQueryAuth implements IQueryAuth {

    @Autowired
    private FrameworkUserMapper frameworkUserMapper;
    /**
     * 加载权限
     *
     * @param userInfo 用户信息
     * @return 返回权限
     */
    @Override
    public Boolean load(UserInfo userInfo) {
        //查询权限
        List<FunctionAuthDto> functionAuthDtos = frameworkUserMapper.queryFuncAndAuthInfoByUserId(userInfo.getUserId());
        if (CollectionUtil.isNotNull(functionAuthDtos)) {
            Set<String> function = new HashSet<>();
            Set<AuthDto> auth = new HashSet<>();
            for (FunctionAuthDto functionAuthDto : functionAuthDtos) {
                 function.add(functionAuthDto.getFuncCode());
                 auth.add(new AuthDto(functionAuthDto.getAuthCode(), functionAuthDto.getAuthName()));
            }
            userInfo.setAuth(auth);
            userInfo.setFunction(function);
        }
        return Boolean.TRUE;
    }
}
